package cn.iocoder.yudao.module.wallet.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.wallet.controller.app.request.TransferEthRequest;
import cn.iocoder.yudao.module.wallet.controller.app.response.TransactionResponse;
import cn.iocoder.yudao.module.wallet.service.eth.EthService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.web3j.utils.Convert;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author liutao
 * @version 1.0
 * @description: TODO
 * @date 2024/10/31 19:18
 */

@Slf4j
@Tag(name = "钱包 App - 钱包管理")
@RestController
@RequestMapping("/wallet/eth")
@Validated
public class EthController {

    @Resource
    private EthService ethService;

    /**
     * 获取账户的Nonce
     *
     * @param addr
     * @return
     */
    @PermitAll
    @GetMapping("/getNonce")
    public CommonResult<BigInteger> getNonce(@RequestParam("addr") String addr) {
        return CommonResult.success(ethService.getNonce(addr));
    }

    @PermitAll
    @GetMapping("/getBalance")
    public CommonResult<BigDecimal> getBalance(@RequestParam("addr") String addr) {
        return CommonResult.success(ethService.getBalance(addr));
    }

    @PermitAll
    @PostMapping("/transfer")
    public CommonResult<String> transfer(@RequestBody TransferEthRequest req) {
        String data = ethService.transfer(req);
        return CommonResult.success(data);
    }

    @PermitAll
    @GetMapping("transaction/{txid}")
    public CommonResult<TransactionResponse> transaction(@PathVariable String txid) {
        return CommonResult.success(ethService.transaction(txid));
    }

    @GetMapping("/gas-price")
    public CommonResult<BigInteger> gasPrice() throws IOException {
        return CommonResult.success(ethService.doGetEthGasPrice());
    }

}
